package com.sun.task;

import com.alibaba.fastjson2.JSON;
import com.sun.handler.MyWebSocketHandler;
import com.sun.pojo.PushEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class ScheduledTask {

    @Autowired
    private MyWebSocketHandler webSocketHandler;

    @Scheduled(fixedRate = 5000) // 每5秒执行一次
    public void sendTimeMessage() {
        PushEntity pushData = new PushEntity();
        pushData.setType("scheduledTask");
        pushData.setData(System.currentTimeMillis());
        webSocketHandler.broadcastMessage("scheduledTask", JSON.toJSONString(pushData));
    }

}